import Mock from 'mockjs'

Mock.setup({
    timeout: '200'
})
let { UserManager, RoleManager} = Mock.mock({
    'UserManager|10': [
        {
            'id|+1': 1,
            Name: '@string("lower", 5)',
            Sex: '@pick(["男","女"])',
            Img: 'http://120.48.115.252:9990/img/error.7cbea5ed.png',
            Role: '@cname(2)' + '管理员',
            Realname: '@cname(2-4)',
            CreateTime: '@datetime',
            Admin: '@cname(2-4)',
            Yn: '@pick([true,false])',
            UpdateTime: '@datetime',
        }
    ],
    'RoleManager|2': [
        {
            "id|1-99": 23,
            UpRole: '@cname(3)' + '管理员',
            Name: '@cname(2-6)',
            "DepName|1-3": 1,
            Yn: '@pick([true,false])',
            Admin: '@cname(2-4)',
            CreateTime: '@datetime',
            User: '@cname(2-4)',
            UpdateTime: '@datetime',
        }
    ],
})

// 用户管理接口
// UserManage
Mock.mock('https://www.vol.com/UserManage', 'get', (option) => {
    return UserManager
})
// id查询接口
Mock.mock(/^https:\/\/www.vol.com\/UserManage\/GetId/, 'get', (option) => {
    let id = option.url.split('?')[1].split('=')[1]
    return UserManager.find(r => r.id == id)
})
// 添加接口
Mock.mock('https://www.vol.com/UserManage/Add', 'post', (option) => {
    if (UserManager.some(r => r.id == JSON.parse(option.body).id)) return false
    else {
        UserManager.push(JSON.parse(option.body));
        return true
    }
})
// 删除接口
Mock.mock('https://www.vol.com/UserManage/Delete', 'post', (option) => {
    UserManager.splice(UserManager.findIndex(r => r.id == JSON.parse(option.body).id), 1)
    return true
})
// 编辑接口
Mock.mock('https://www.vol.com/UserManage/Edit', 'post', (option) => {
    let obj = JSON.parse(option.body)

    let sub = UserManager.find(s => s.id == obj.id)
    if (sub) {
        sub.id = obj.id
        sub.Name = obj.Name
        sub.Sex = obj.Sex
        sub.img = obj.img
        sub.Role = obj.Role
        sub.Realname = obj.Realname
        sub.CreateTime = obj.CreateTime
        sub.Admin = obj.Admin
        sub.Yn = obj.Yn
        sub.UpdateTime = obj.UpdateTime
        return true
    } else {
        return false
    }

})
// 分页和模糊查询
Mock.mock(/^https:\/\/www.vol.com\/UserManage\/Page/, 'get', (option) => {
    console.log(option);
    let search = option.url.split('?')[1]
    let params = {}
    search.split('&').forEach(r => {
        let arr = r.split('=')
        params[arr[0]] = arr[1]
    })
    let { PageIndex, PageSize, Name } = params
    let string = decodeURIComponent(Name)
    if (string == '') {
        return {
            count: UserManager.length,
            data: UserManager.slice((PageIndex - 1) * PageSize, PageIndex * PageSize)
        }
    } else {
        return {
            data: mohuchaxun2(string, UserManager).slice((PageIndex - 1) * PageSize, PageIndex * PageSize),
            count: UserManager.length
        }
    }

})

// 角色管理接口
// RoleManager
Mock.mock('https://www.vol.com/RoleManager', 'get', (option) => {
    return RoleManager
})
// id查询接口
Mock.mock(/^https:\/\/www.vol.com\/RoleManager\/GetId/, 'get', (option) => {
    let id = option.url.split('?')[1].split('=')[1]
    return RoleManager.find(r => r.id == id)
})
// 添加接口
Mock.mock('https://www.vol.com/RoleManager/Add', 'post', (option) => {
    if (RoleManager.some(r => r.id == JSON.parse(option.body).id)) return false
    else {
        RoleManager.push(JSON.parse(option.body));
        return true
    }
})
// 删除接口
Mock.mock('https://www.vol.com/RoleManager/Delete', 'post', (option) => {
    RoleManager.splice(RoleManager.findIndex(r => r.id == JSON.parse(option.body).id), 1)
    return true
})
// 编辑接口
Mock.mock('https://www.vol.com/RoleManager/Edit', 'post', (option) => {
    let obj = JSON.parse(option.body)
    let sub = RoleManager.find(s => s.id == obj.id)
    if (sub) {
        sub.id = obj.id
        sub.UpRole = obj.UpRole
        sub.Name = obj.Name
        sub.DepName = obj.DepName
        sub.Yn = obj.Yn
        sub.Admin = obj.Admin
        sub.CreateTime = obj.CreateTime
        sub.User = obj.User
        sub.UpdateTime = obj.UpdateTime
        return true
    } else {
        return false
    }

})
// 分页和模糊查询
Mock.mock(/^https:\/\/www.vol.com\/RoleManager\/Page/, 'get', (option) => {
    // console.log(option);
    let search = option.url.split('?')[1]
    let params = {}
    search.split('&').forEach(r => {
        let arr = r.split('=')
        params[arr[0]] = arr[1]
    })
    let { PageIndex, PageSize, Name } = params
    let string = decodeURIComponent(Name)
    if (string == '') {
        return {
            count: RoleManager.length,
            data: RoleManager.slice((PageIndex - 1) * PageSize, PageIndex * PageSize)
        }
    } else {
        return {
            data: mohuchaxun2(string, RoleManager).slice((PageIndex - 1) * PageSize, PageIndex * PageSize),
            count: RoleManager.length
        }
    }

})
// 模糊查询函数
function mohuchaxun2(name, object) {
    let obj = []
    let a = []
    object.forEach(r => {
        a.push(r.Name)
    })
    let arr = [];
    for (var i = 0; i < a.length; i++) {
        if (a[i].indexOf(name) >= 0) {
            arr.push(a[i]);
        }
    }
    arr.forEach(r => {
        obj.push(object.find(s => s.Name == r))
    })
    return obj
}
export default Mock